<?php
/**
 * Created by PhpStorm.
 * User: 轩
 * Date: 2017/8/26
 * Time: 16:04
 */

namespace app\home\service;

use app\home\common\Tools;
use app\home\common\WechatSDK;
use app\home\common\WechatBase;

class MiniappService extends WechatBase
{
    /*
     * 概况趋势
     * @Xiao 2017年9月2日16:40:19 改写
     */
    public function userSum($type, $miniID, $date)
    {
        if (empty($type) or empty($miniID)) {
            return false;
        }
        $AS = new AccountService();
        $appid = $AS->getminiAppsData('appid', $miniID);
        $appsecrt = $AS->getminiAppsData('secret', $miniID);

        if (empty($appid) or empty($appsecrt)) {
            return false;
        }

        $SDK = new WechatSDK($appid, $appsecrt);
        $token = $SDK->getAccessToken();
        $Tools = new Tools();

        switch ($type) {
            case 'sum':
                $data = $SDK->getSum($token, $date);
                return (isset($data->errcode) or empty($data->list)) ? [
                    "ref_date" => $date,
                    "visit_total" => 0,
                    "share_pv" => 0,
                    "share_uv" => 0,
                ] : $Tools->objarray_to_array($data->list[0]);
                break;

        }
        return false;
    }

    /*
     * 访问趋势
     * @Xiao 2017年9月2日16:40:19 改写
     */
    public function getVisit($type, $miniID, $begin_date, $end_date = null)
    {
        if (empty($type) or empty($miniID)) {
            return false;
        }
        $AS = new AccountService();
        $appid = $AS->getminiAppsData('appid', $miniID);
        $appsecrt = $AS->getminiAppsData('secret', $miniID);

        if (empty($appid) or empty($appsecrt)) {
            return false;
        }

        $SDK = new WechatSDK($appid, $appsecrt);
        $token = $SDK->getAccessToken();
        $Tools = new Tools();
        // 根据类型使用
        switch ($type) {
            case 'dayVisit':  //日趋势
                $data = $SDK->dailyVisit($token, $begin_date);
//                return $data;
                return (isset($data->errcode) or empty($data->list)) ? [
                    "ref_date" => $begin_date,
                    "session_cnt" => 0,
                    "visit_pv" => 0,
                    "visit_uv" => 0,
                    "visit_uv_new" => 0,
                    "stay_time_uv" => 0,
                    "stay_time_session" => 0,
                    "visit_depth" => 0
                ] : $Tools->objarray_to_array($data->list[0]);
                break;
            case 'weekVisit':  //周趋势
                $data = $SDK->weeklyVisit($token, $begin_date, $end_date);
                return (isset($data->errcode) or empty($data->list)) ? [
                    "ref_date" => $begin_date . "-" . $end_date,
                    "session_cnt" => 0,
                    "visit_pv" => 0,
                    "visit_uv" => 0,
                    "visit_uv_new" => 0,
                    "stay_time_uv" => 0,
                    "stay_time_session" => 0,
                    "visit_depth" => 0
                ] : $Tools->objarray_to_array($data->list[0]);
                break;
            case 'monthVisit':  //月趋势
                $data = $SDK->monthlyVisit($token, $begin_date, $end_date);
                return (isset($data->errcode) or empty($data->list)) ? [
                    "ref_date" => substr($begin_date, 0, 6),
                    "session_cnt" => 0,
                    "visit_pv" => 0,
                    "visit_uv" => 0,
                    "visit_uv_new" => 0,
                    "stay_time_uv" => 0,
                    "stay_time_session" => 0,
                    "visit_depth" => 0
                ] : $Tools->objarray_to_array($data->list[0]);
                break;
        }
        return false;
    }

    /*
     * 访问分布
     */
    public function visitDistribute($type, $miniID, $date)
    {
        if (empty($type) or empty($miniID)) {
            return false;
        }
        $AS = new AccountService();
        $appid = $AS->getminiAppsData('appid', $miniID);
        $appsecrt = $AS->getminiAppsData('secret', $miniID);

        if (empty($appid) or empty($appsecrt)) {
            return false;
        }

        $SDK = new WechatSDK($appid, $appsecrt);
        $token = $SDK->getAccessToken();

        switch ($type) {
            case 'distribution':
                $data = $SDK->distribution($token, $date);
                return $data ? $data : null;
                break;

        }
        return false;
    }

    /*
     * 访问留存
     */
    public function visitKeep($type, $miniID, $begin_date, $end_date = null)
    {
        if (empty($type) or empty($miniID)) {
            return false;
        }
        $AS = new AccountService();
        $appid = $AS->getminiAppsData('appid', $miniID);
        $appsecrt = $AS->getminiAppsData('secret', $miniID);

        if (empty($appid) or empty($appsecrt)) {
            return false;
        }

        $SDK = new WechatSDK($appid, $appsecrt);
        $token = $SDK->getAccessToken();

        switch ($type) {

            case 'dayRetain':  //日留存
                $data = $SDK->dailyRetain($token, $begin_date);
                return $data ? $data : null;
                break;
            case 'weekRetain':  //周留存
                $data = $SDK->weeklyRetain($token, $begin_date, $end_date);
                return $data ? $data : null;
                break;
            case 'monthRetain':  //月留存
                $data = $SDK->monthlyRetain($token, $begin_date, $end_date);
                return $data ? $data : null;
                break;
        }
        return false;
    }

    /*
     * 访问页面
     */
    public function visitPage($type, $miniID, $begin_date)
    {
        if (empty($type) or empty($miniID)) {
            return false;
        }
        $AS = new AccountService();
        $appid = $AS->getminiAppsData('appid', $miniID);
        $appsecrt = $AS->getminiAppsData('secret', $miniID);

        if (empty($appid) or empty($appsecrt)) {
            return false;
        }
        $SDK = new WechatSDK($appid, $appsecrt);
        $token = $SDK->getAccessToken();

        switch ($type) {

            case 'visitPage':
                $data = $SDK->visitPage($token, $begin_date);
                return $data ? $data : null;
                break;

        }
        return false;
    }

    /*
     * 用户画像
     */
    public function getUserPort($type, $miniID, $begin_date, $end_date)
    {
        if (empty($type) or empty($miniID)) {
            return false;
        }
        $AS = new AccountService();
        $appid = $AS->getminiAppsData('appid', $miniID);
        $appsecrt = $AS->getminiAppsData('secret', $miniID);

        if (empty($appid) or empty($appsecrt)) {
            return false;
        }

        $SDK = new WechatSDK($appid, $appsecrt);
        $token = $SDK->getAccessToken();
        $data = $SDK->userPortrait($token, $begin_date, $end_date);
        $Tools = new Tools();
        switch ($type) {
            case 'new':
                return (isset($data->errcode) or empty($data->visit_uv_new)) ? 0 : $Tools->objarray_to_array($data)['visit_uv_new'];
                break;
            case 'live':
                return (isset($data->errcode) or empty($data->visit_uv)) ? 0 : $Tools->objarray_to_array($data)['visit_uv'];
                break;
        }
        return false;
    }

}
